package com.weixing.mall.provider.controller.front;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.support.auth.MemInfo;
import com.weixing.mall.provider.model.dto.BuyerShowChgStatusDto;
import com.weixing.mall.provider.model.dto.UploadBuyerShowDto;
import com.weixing.mall.provider.service.IBuyerShowService;
import com.weixing.mall.provider.service.IPraiseLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName FrontBuyerShowController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/11/14
 * @Version 1.0
 **/
@RestController
@RequestMapping("front/buyershow")
@Api(value = "前端：App端买家秀api", tags = "前端：买家秀")
public class FrontBuyerShowController extends BaseController {

    @Autowired
    private IBuyerShowService buyerShowService;
    @Autowired
    private IPraiseLogService praiseLogService;

    @ApiOperation(value = "买家秀列表")
    @GetMapping("getList/{goodsId}")
    @NoNeedAccess
    public Result list(@PathVariable Long goodsId) {
        MemAuth memberExt = AuthUtil.getMemberExt();
        Long mid = null == memberExt ? null : memberExt.getMid();
        return ResultUtil.success(buyerShowService.selectListOfPage(initMpPage(), goodsId,mid));
    }

    @ApiOperation(value = "买家秀列表")
    @GetMapping("getList")
    public Result allList() {
        MemAuth memberExt = AuthUtil.getMemberExt();
        Long mid = null == memberExt ? null : memberExt.getMid();
        return ResultUtil.success(buyerShowService.selectListOfPage(initMpPage(), null,mid));
    }

    @ApiOperation(value = "上传买家秀")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result uploadBuyerShow(@MemInfo MemAuth auth,@RequestBody UploadBuyerShowDto uploadBuyerShowDto) {
        buyerShowService.uploadBuyerShow(uploadBuyerShowDto,auth.getMid());
        return ResultUtil.success();
    }

    /**
     * 进行了调整了，修改为flag 和买家秀id
     * @param buyerShowChgStatusDto
     * @return
     */
    @ApiOperation(value = "修改点赞数(只需要传买家秀id和flag)")
    @PostMapping("chgPraiseCount")
    public Result chgPraiseCount(@MemInfo MemAuth auth,@RequestBody BuyerShowChgStatusDto buyerShowChgStatusDto) {
        if (buyerShowChgStatusDto.getFlag() == null || buyerShowChgStatusDto.getBuyerShowId() == null || auth.getMid() == null) {
            return ResultUtil.error();
        }
        if(buyerShowChgStatusDto.getFlag() == 1 && praiseLogService.checkPraise(buyerShowChgStatusDto.getBuyerShowId(),auth.getMid())){
            return ResultUtil.error("已点赞");
        }
        buyerShowService.updateStatus(buyerShowChgStatusDto);
        //由于上边updateStatus是与后台共用，所以记录只能单独处理
        if(buyerShowChgStatusDto.getFlag() == 1 ){
            praiseLogService.addPraiseLog(buyerShowChgStatusDto.getBuyerShowId(),auth.getMid());
        }else{
            praiseLogService.delPraiseLog(buyerShowChgStatusDto.getBuyerShowId(),auth.getMid());
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "查询买家秀详情")
    @GetMapping("queryDetails/{id}")
    @NoNeedAccess
    public Result queryDetails(@PathVariable Long id) {
        return ResultUtil.success(buyerShowService.queryDetails(id));
    }

}
